﻿@page "/System/Role"
@attribute [TabItemOption(Text = "角色管理")]
@using System.Diagnostics.CodeAnalysis;
@using BootstrapBlazorApp.Shared.Data.SYS;
@inject MessageService msg

<Table TItem="RoleDto" PageItemsSource="@PageItemsSource" AutoGenerateColumns="true"
       IsPagination="true" IsStriped="true" IsBordered="true" IsMultipleSelect="true"
       ShowToolbar="true" ShowExtendButtons="true" ShowLoading="true" ShowSearch="true"
       OnAddAsync="@OnAddAsync" OnEditAsync="@OnEditAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync"
       OnQueryAsync="@OnQueryAsync" ShowExportButton="true">
    <TableColumns>

    </TableColumns>
</Table>

@code {
    /// <summary>
    /// 当前Token信息
    /// </summary>
    [CascadingParameter]
    private ITokenContext? Context { get; set; }


    private static IEnumerable<int> PageItemsSource => new int[] { 4, 10, 20 };


    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

    }

    private static async Task<RoleDto> OnAddAsync()
    {
        // 模拟延时

        return new RoleDto() { };
    }

    private static async Task<RoleDto> OnEditAsync(RoleDto item)
    {
        // 模拟延时
        return item;
    }

    private async Task<bool> OnSaveAsync(RoleDto item, ItemChangedType changedType)
    {
        if (changedType == ItemChangedType.Add)
        {
            item.Creater = Context.CurrentUser.Account;
            item.CreatedName = Context.CurrentUser.UserName;
            return await Global.ExecuteApiAsync(() => Request.System.AddRoleAsync(item));
        }
        else
        {
            item.Modifier = Context.CurrentUser.UserName;
            item.ModifiedName = Context.CurrentUser.UserName;
            return await Global.ExecuteApiAsync(() => Request.System.UpdateRoleAsync(item));
        }
    }

    private async Task<bool> OnDeleteAsync(IEnumerable<RoleDto> items)
    {
        return await Global.ExecuteApiAsync(() => Request.System.DeleteMutiRoleAsync(items.ToList()));
    }

    private async Task<QueryData<RoleDto>> OnQueryAsync(QueryPageOptions options)
    {
        try
        {
            var model = await Request.System.QueryRoleAsync(QueryParameter.GetQueryParameter(options));
            return model.data;
        }
        catch (Exception ex)
        {
            return new QueryData<RoleDto>();
        }
    }
}
